package _5thread;

import java.util.concurrent.locks.LockSupport;

/**
 * @description:
 * @author: wang zhen
 * @create: 2022-06-14 15:17
 */
public class LockSupportTest {

    public static void main(String[] args){


        //消费者线程消费10个包子
        Thread consume=new Thread("消费者"){

            @Override
            public void run() {

                for(int i=1;i<=10;i++){
                    LockSupport.park();
                    System.out.println(Thread.currentThread().getName()+"消费了第"+i+"个");
                }

            }
        };

        Thread prod=new Thread("生产者"){

            @Override
            public void run() {

                for(int i=1;i<=10;i++){

                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    System.out.println(Thread.currentThread().getName()+"生产了第"+i+"个");
                    LockSupport.unpark(consume);
                }
            }
        };


        consume.start();
        prod.start();
    }


}
